package mobi.hami.huapin.util;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import io.swagger.util.Json;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.FatalBeanException;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhumaer
 * @desc Bean操作工具类（拷贝方法时支持添加{@link FieldAlias} 注解）
 * @since 7/6/2017 3:13 PM
 */
public class BeanUtil {

    public static void copyProperties(Object source, Object target, String... ignoreProperties) throws BeansException {

        Assert.notNull(source, "Source must not be null");
        Assert.notNull(target, "Target must not be null");

        Class<?> actualEditable = target.getClass();
        PropertyDescriptor[] targetPds = BeanUtils.getPropertyDescriptors(actualEditable);
        List<String> ignoreList = (ignoreProperties != null ? Arrays.asList(ignoreProperties) : null);

        for (PropertyDescriptor targetPd : targetPds) {
            Method writeMethod = targetPd.getWriteMethod();
            if (writeMethod != null && (ignoreList == null || !ignoreList.contains(targetPd.getName()))) {
                PropertyDescriptor sourcePd = getPropertyDescriptor(source.getClass(), target.getClass(), targetPd);
                if (sourcePd != null) {
                    Method readMethod = sourcePd.getReadMethod();
                    if (readMethod != null && ClassUtils.isAssignable(writeMethod.getParameterTypes()[0], readMethod.getReturnType())) {
                        try {
                            if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
                                readMethod.setAccessible(true);
                            }
                            Object value = readMethod.invoke(source);
                            if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers())) {
                                writeMethod.setAccessible(true);
                            }
                            writeMethod.invoke(target, value);
                        } catch (Throwable ex) {
                            throw new FatalBeanException("Could not copy property '" + targetPd.getName() + "' from source to target", ex);
                        }
                    }
                }
            }
        }

    }

    private static PropertyDescriptor getPropertyDescriptor(Class<?> sourceClazz, Class<?> targetClazz, PropertyDescriptor targetPd) {
        PropertyDescriptor sourcePd = BeanUtils.getPropertyDescriptor(sourceClazz, targetPd.getName());
        if (sourcePd == null) {
            try {
                Field field = getDeclaredField(targetClazz, targetPd.getName());
                FieldAlias[] fieldAliases = field.getAnnotationsByType(FieldAlias.class);
                if (fieldAliases == null) {
                    return null;
                }

                for (FieldAlias fieldAlias : fieldAliases) {
                    if (ArrayUtils.isEmpty(fieldAlias.sourceClass()) || ArrayUtils.contains(fieldAlias.sourceClass(), sourceClazz)) {
                        sourcePd = BeanUtils.getPropertyDescriptor(sourceClazz, fieldAlias.value());
                        if (sourcePd != null) {
                            return sourcePd;
                        }
                    }
                }
            } catch (NoSuchFieldException | SecurityException e) {
                e.printStackTrace();
            }
        }
        return sourcePd;
    }

    /**
     * 获取某个类下的属性（包含父类）
     */
    public static Field getDeclaredField(Class<?> targetClazz, String fieldName) throws NoSuchFieldException {
        Class<?> clazz = targetClazz;
        while (clazz != Object.class) {
            try {
                return clazz.getDeclaredField(fieldName);
            } catch (NoSuchFieldException e) {
                clazz = clazz.getSuperclass();
            }
        }

        throw new NoSuchFieldException();
    }


    /**
     * 比较两个对象的值是否相同
     * @param t1
     * @param t2
     * @param <T>
     * @return　相同返回为true,否则为false
     */
    public static <T> boolean CompareBean(T t1, T t2) {
       return   JSON.toJSONString(t1).equals(JSON.toJSONString(t2));

    }

    /**
     * 将分页的数据进行封装，返回Map对象，包含分页信息
     *
     * @param page　page对象
     * @param list　分页后的数据集合
     * @param pageSize
     * @param pageIndex
     * @return
     */
    public static  Map<String, Object> ResultResponseMap(Page page, Object list, Integer pageSize, Integer pageIndex) {
        Map<String, Object> resultMap = new HashMap<>();
        int modelSize = 3;
        if (page != null) {
            modelSize = (int) page.getTotal();
        }
        resultMap.put("totalNum", modelSize);
        int totalPageNum = (modelSize + pageSize - 1) / pageSize;
        resultMap.put("totalPageNum", totalPageNum);
        resultMap.put("startPageNum", pageIndex);
        resultMap.put("pageSize", pageSize);
        resultMap.put("list", list);
        return resultMap;
    }

}
